package com.ss.android.article.base.feature.feed.presenter;

import android.content.SharedPreferences;

import com.bytedance.common.utility.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;

public final class LastReadDataManager {
    public static final class LastReadData {
        public long time;
        public long behotTime;
        public long nextBehotTime;
        public String categoryId;
        public boolean clickable;
        public long cursor;

        public LastReadData() {

        }

        static JSONObject a(LastReadData data) {
            JSONObject v0;
            JSONObject v1 = null;
            if(data != null
                    && data.time > 0
                    && !android.support.design.a.isTextEmpty(data.categoryId)) {
                v0 = new JSONObject();
                try {
                    v0.put("time", data.time);
                    v0.put("behot_time", data.behotTime);
                    v0.put("next_behot_time", data.nextBehotTime);
                    v0.put("categoryId", data.categoryId);
                    v0.put("clickable", data.clickable);
                    v0.put("next_behot_time", data.nextBehotTime);
                    v0.put("cursor", data.cursor);
                }
                catch(JSONException v0_1) {
                    v0_1.printStackTrace();
                    v0 = v1;
                }
            }
            else {
                v0 = v1;
            }

            return v0;
        }
    }

    public HashMap a;
    public SharedPreferences b;
    private static WeakReference c;
    private static Object d = new Object();

    private LastReadDataManager(SharedPreferences sharedPreferences) {
        this.b = sharedPreferences;
        this.a = LastReadDataManager.parseAll(this.b.getString("sp_key_last_read", ""));
        if(this.a == null) {
            this.a = new HashMap();
        }
    }

    public final LastReadData a(String arg3) {
        LastReadData v0;
        LastReadData v1 = null;
        if(android.support.design.a.isTextEmpty(arg3)) {
            v0 = v1;
            return v0;
        }

        if(this.a == null) {
            return v1;
        }

        try {
            v0 = (LastReadData) this.a.get(arg3);
        }
        catch(Throwable v0_1) {
            v0_1.printStackTrace();
            v0 = v1;
        }

        return v0;
    }

    public static String a(HashMap arg4) {
        String v0_1;
        if(arg4 == null || arg4.size() == 0) {
            v0_1 = "";
        }
        else {
            JSONArray v1 = new JSONArray();
            Iterator v2 = arg4.values().iterator();
            while(v2.hasNext()) {
                JSONObject v0 = LastReadData.a((LastReadData) v2.next());
                if(v0 == null) {
                    continue;
                }

                v1.put(v0);
            }

            v0_1 = v1.toString();
            Logger.d("LastReadDataManager", "serilizeAll:" + v0_1);
        }

        return v0_1;
    }

    public static LastReadDataManager a(SharedPreferences arg3) {
        Object v1 = null;
        Object v0 = LastReadDataManager.c == null ? v1 : LastReadDataManager.c.get();
        if (v0 == null) {
            Object v2 = LastReadDataManager.d;
            synchronized (v2) {
                v0 = LastReadDataManager.c == null ? v1 : LastReadDataManager.c.get();
                if (v0 == null) {
                    v0 = new LastReadDataManager(arg3);
                    LastReadDataManager.c = new WeakReference(v0);
                }
            }
        }
        return ((LastReadDataManager) v0);
    }

    private static HashMap parseAll(String arg20) {
        LastReadData v3_1;
        HashMap v2;
        Logger.d("LastReadDataManager", "parseAll:" + arg20);
        if(android.support.design.a.isTextEmpty(arg20)) {
            v2 = null;
            return v2;
        }

        try {
            JSONArray v5 = new JSONArray(arg20);
            int v6 = v5.length();
            if(v6 == 0) {
                return null;
            }

            v2 = new HashMap();
            int v4;
            for(v4 = 0; v4 < v6; ++v4) {
                JSONObject v3 = v5.getJSONObject(v4);
                if(v3 == null) {
                    v3_1 = null;
                }
                else {
                    long v8 = v3.optLong("time");
                    long v10 = v3.optLong("behot_time");
                    long v12 = v3.optLong("next_behot_time");
                    String v7 = v3.optString("categoryId");
                    boolean v14 = v3.optBoolean("clickable");
                    long v16 = v3.optLong("cursor");
                    if(v10 > 0 && v8 > 0 && !android.support.design.a.isTextEmpty(v7)) {
                        v3_1 = new LastReadData();
                        v3_1.time = v8;
                        v3_1.behotTime = v10;
                        v3_1.categoryId = v7;
                        v3_1.clickable = v14;
                        v3_1.nextBehotTime = v12;
                        v3_1.cursor = v16;
                    }else {
                        v3_1 = null;
                    }
                }

            label_27:
                if(v3_1 != null) {
                    v2.put(v3_1.categoryId, v3_1);
                }
            }

            Logger.d("LastReadDataManager", "解析成功,有 " + v2.size() + " 个元素");
        }
        catch(JSONException v2_1) {
            v2_1.printStackTrace();
            v2 = null;
        }

        return v2;
    }
}

